﻿package com.webdesign.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.webdesign.dao.mapper.TbUserMapper;
import com.webdesign.dto.UserDTO;
import com.webdesign.entity.TbUser;
import com.webdesign.entity.TbUserExample;
import com.webdesign.service.ITbUserService;
import com.webdesign.util.MD5;

@Service("TbUserService")
public class TbUserService implements ITbUserService {
	@Autowired
	private TbUserMapper tbUserMapper;

	/*
	 * 用户登录
	 */
	@Override
	public TbUser userLogin(String userid, String password) {
		TbUser tbUser = tbUserMapper.selectByPrimaryKey(userid);
		if (tbUser != null) {
			if (tbUser.getPassword().equals(password)) {
				return tbUser;
			} else {
				TbUser user = null;
				return user;
			}
		} else {
			TbUser user = null;
			return user;
		}
	}

	/*
	 * 往数据库中输入学生信息，先判断该学生是否存在
	 */
	@Override
	public void insertUsers(String termid, List<TbUser> tbUserList) {

		for (TbUser tbUser : tbUserList) {
			// 密码初始化为学号，用MD5加密
			MD5 md = new MD5();
			String password = md.MD5(tbUser.getUserid());

			tbUser.setTermid(termid);
			tbUser.setPassword(password);

			// 判断该学生是否存在，若不存在则新建学生
			if (tbUserMapper.selectByPrimaryKey(tbUser.getUserid()) == null) {
				tbUserMapper.insertSelective(tbUser);
			}
		}

	}

	/*
	 * 根据termid返回该学年所有学生的信息
	 */
	@Override
	public List<TbUser> showUsers(String termid,String teacherId) {
		TbUserExample tbUserExample = new TbUserExample();
		tbUserExample.createCriteria().andTermidEqualTo(termid).andTeacheridEqualTo(teacherId);
		List<TbUser> users = tbUserMapper.selectByExample(tbUserExample);
		
		return users;
	}

	/*
	 * 根据学生id批量重置学生密码
	 */
	@Override
	public Boolean resetPassword(String[] userids) {
		MD5 md = new MD5();
		Boolean flag = true;
		for (int i = 0; i < userids.length; i++) {
			TbUser tbUser = tbUserMapper.selectByPrimaryKey(userids[i]);
			TbUser user = new TbUser();
			user.setUserid(tbUser.getUserid());
			user.setUsername(tbUser.getUsername());

			String password = md.MD5(tbUser.getUserid());
			user.setPassword(password);

			user.setTermid(tbUser.getTermid());
			user.setTeacherid(tbUser.getTeacherid());
			if (tbUserMapper.updateByPrimaryKey(user) == 0) {
				flag = false;
			}
		}
		return flag;
	}
	

	/*
	 * 统计学年学生人数
	 */
	@Override
	public int countUser(String termid) {
		// TODO Auto-generated method stub
		TbUserExample tbUserExample = new TbUserExample();
		tbUserExample.createCriteria().andTermidEqualTo(termid);
		return tbUserMapper.countByExample(tbUserExample);
	}

	/*
	 * 学生修改密码
	 */
	@Override
	public TbUser stuRewrite(String userid, String password, String newpwd) {
		/*
		 * userid,学号
		 * password,经过加密后的原密码
		 * newpwd,经过加密后的新密码
		 */
		
		TbUser tbUser = tbUserMapper.selectByPrimaryKey(userid);
		if (tbUser != null) {
			if (tbUser.getPassword().equals(password)) {
				TbUser user = new TbUser();
				user.setUsername(tbUser.getUsername());
				user.setUserid(userid);
				user.setPassword(newpwd);
				user.setTermid(tbUser.getTermid());
				tbUserMapper.updateByPrimaryKey(user);
				return user;
			} else {
				TbUser user = null;
				return user;
			}
		} else {
			TbUser user = null;
			return user;
		}
	}

}
